package com.lbt.mybatis.plus.extend.demo.mybatis;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lbt.mybatis.plus.extend.bean.QueryFieldValueItemParent;
import com.lbt.mybatis.plus.extend.json.MybatisPlusExtendJsonDeserializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

/**
 * @author zhangjinyu
 * @since 2023-05-16
 */
@Slf4j
@Component
public class DemoMybatisPlusExtendJsonDeserializer implements MybatisPlusExtendJsonDeserializer {
    final ObjectMapper objectMapper;
    JavaType javaType;

    public DemoMybatisPlusExtendJsonDeserializer(ObjectMapper objectMapper) {
        javaType = objectMapper.getTypeFactory().constructParametricType(List.class, QueryFieldValueItemParent.class);
        this.objectMapper = objectMapper;
    }

    @Override
    public List<QueryFieldValueItemParent> queryFieldDeserialize(String queryFieldJson) {
        try {
            return objectMapper.readValue(queryFieldJson, javaType);
        } catch (JsonProcessingException e) {
            log.error("QueryField反序列化失败", e);
            return Collections.emptyList();
        }
    }
}
